<?php

namespace App\Model;

use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * Class Operators
 *
 * @package App\Model
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Query\Builder|\App\Model\Operators onlyTrashed()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Query\Builder|\App\Model\Operators withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Model\Operators withoutTrashed()
 * @mixin \Eloquent
 * @property-read \App\User $user
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators query()
 * @property int $id 主键
 * @property int $place_partner_id 地区合伙人id
 * @property string|null $business_license 营业执照地址
 * @property int|null $province_id
 * @property int $user_id
 * @property int|null $city_id
 * @property int|null $area_id
 * @property string $title 运营商名字
 * @property string $name 负责人名字
 * @property string $phone
 * @property string $id_card 身份证
 * @property string $bank_card_no 银行卡号
 * @property string $bank_card_name 银行卡账户名
 * @property string $bank_name 银行卡开户行
 * @property string $bank_branch 支行
 * @property int|null $salesman_count 业务员数量
 * @property int|null $user_count 用户数量
 * @property int|null $vip_count 会员数量
 * @property \Illuminate\Support\Carbon $created_at 创建时间
 * @property \Illuminate\Support\Carbon $updated_at 创建时间
 * @property string|null $deleted_at 删除时间
 * @property int $status
 * @property int $examine 审核0未通过1通过
 * @property string $examined_at
 * @property float $agency_fee 代理费
 * @property string|null $referee_name 推荐人姓名
 * @property string|null $referee_phone 推荐人电话号码
 * @property string $id_card_front 身份证正面
 * @property string $id_card_back 身份证反面
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereAgencyFee($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereAreaId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereBankBranch($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereBankCardName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereBankCardNo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereBankName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereBusinessLicense($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereCityId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereExamine($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereExaminedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereIdCard($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereIdCardBack($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereIdCardFront($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators wherePlacePartnerId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereProvinceId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereRefereeName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereRefereePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereSalesmanCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereUserCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Operators whereVipCount($value)
 */
class Operators extends Model
{
    use SoftDeletes;
    protected $table = 'operators';

    protected $fillable = [
        'place_partner_id',
        'province_id',
        'city_id',
        'area_id',
        'title',
        'name',
        'phone',
        'id_card',
        'bank_card_no',
        'bank_card_name',
        'bank_name',
        'bank_branch',
        'agency_fee',
        'business_license','id_card_front','id_card_back','referee_name','referee_phone'
    ];
    public function user()
    {
        return $this->hasOne(User::class, 'user_id', 'id');
    }

    protected $hidden = ['deleted_at'];

    public function getPaginate(Request $request, $where)
    {
        if ($request->has('province_id')) {
            $where['province_id'] = $request->input('request');
        }
        if ($request->has('city_id')) {
            $where['city_id'] = $request->input('city_id');
        }
        if ($request->has('name')) {
            $where['name'] = $request->input('name');
        }
        if ($request->has('title')) {
            $where['title'] = $request->input('title');
        }
        if ($request->has('phone')) {
            $where['phone'] = $request->input('phone');
        }
        $limit = request('limit', 15);
        return $this->where($where)->paginate($limit)->toArray();
    }

    public function examines()
    {
        $o = $this->leftJoin('place_partners', 'operators.place_partner_id', '=', 'place_partners.id');
        $o->select('operators.id', 'operators.title as o_title','place_partners.title as p_title', 'operators.created_at as created_at', 'operators.examined_at', 'operators.examine', 'operators.agency_fee');
        if (request()->has('examine')){
            $o->where('operators.examine', '=', request('examine'));
        }
        if (request()->has('phone')){
            $o->where('phone', '=', request('phone'));
        }
        if (request()->has('name')){
            $name = request('name');
            $o->where(function ($query) use ($name){
                $query->where('operators.name', 'like', "%$name%")->orWhere('operators.title' ,'like', "%$name%")->orWhere('place_partners.title', 'like', "$name");
            });
        }
        if (request()->has('start')){
            $o->where('operators.created_at', '>=', request('start'));
        }
        if (request()->has('end')){
            $o->where('operators.created_at', '<=', request('end'));
        }
        if (request()->has('start') and request()->has('end')){
            $o->whereBetween('operators.created_at', [request('start'), request('end')]);
        }
        return $o->paginate(15);
    }
}
